/*************************************
Copyright 2011 Chinua Iloabachie, Pradnya Khutafale, Siyi Liu, Junichi Suzuki, and The University of Massachusetts, Boston (http://code.google.com/p/tabletuml/)


Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 
*************************************/



package touchMe.Draw;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import touchMe.Util.DiagramFileHandler;
import touchMe.Util.DiagramManager;
import touchMe.baseClass.Constants;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class OpenService extends Activity {
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            if (getIntent().getScheme().equals("content")) {
                    InputStream is = getContentResolver().openInputStream(getIntent().getData());
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                    StringBuffer buf=new StringBuffer();
                    
                    while (true) {
                            String line = reader.readLine();
                            if(line !=null){
                            	buf.append(line);
                            	buf.append("\n");
                            	//Log.d(Constants.TAG, line);
                            }
                            else {
                            	//String filePath=DiagramFileHandler.writeTempFile(buf.toString());
                            	DiagramManager.parseString(buf.toString());
                            	Constants.explorerMode=1;
                            	Intent openIntent = new Intent(OpenService.this,TouchMe.class);
                            	//openIntent.putExtra(TouchMe.filePath,filePath);
                                startActivity(new Intent(openIntent));
                             	break;
                            }
    	        }
    	    }
    	} catch (FileNotFoundException FnFe) {
    		Log.d(Constants.TAG,"FnFe: "+FnFe.getMessage());
    	} catch (IOException IOE) {
    		Log.d(Constants.TAG,"IOE: "+IOE.getMessage());
    	}
	}
}
